package com.wj.dubbo.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.wj.console.IWelfareShopService;
import com.wj.console.bean.welfareshop.param.CreateOrderParam;
import com.wj.console.bean.welfareshop.param.ProductQuery;
import com.wj.console.bean.welfareshop.param.SalesReturnOrNifferApplyParam;
import com.wj.console.bean.welfareshop.result.Address;
import com.wj.console.bean.welfareshop.result.AfterSaleDetail;
import com.wj.console.bean.welfareshop.result.Cate;
import com.wj.console.bean.welfareshop.result.CommodityStatus;
import com.wj.console.bean.welfareshop.result.CreateOrderResult;
import com.wj.console.bean.welfareshop.result.LogisticsMsg;
import com.wj.console.bean.welfareshop.result.OrderDetail;
import com.wj.console.bean.welfareshop.result.Price;
import com.wj.console.bean.welfareshop.result.ProductDetail;
import com.wj.console.bean.welfareshop.result.Remain;
import com.wj.console.bean.welfareshop.result.SalesReturnMsg;
import com.wj.console.bean.welfareshop.result.SalesReturnOrNifferApplyResult;
import com.wj.console.bean.welfareshop.result.Stock;
import com.wj.console.common.Page;
import com.wj.console.common.exception.BusinessException;
import com.wj.console.rpc.RpcResponse;
import com.wj.dubbo.service.welfareshop.service.impl.ProductServiceImpl;
import com.wj.dubbo.service.welfareshop.service.impl.WelfareShopBasicMsgService;
import com.wj.dubbo.service.welfareshop.service.impl.WelfareShopCallService;

/**   
 * 类名称：IwelfareShopCallService
 * 类描述 ：福利商城服务接口实现
 * 创建人: liuweidong  
 * 创建时间：2017-5-3 上午11:33:05  
 * 修改人：
 * 修改时间：
 * 修改备注：
 * 版本： V1.0
 */
@Service(timeout=5000, connections=10)
public class WelfareShopService implements IWelfareShopService {
	private static final Logger log = LoggerFactory.getLogger(WelfareShopService.class);
	//@Reference(url="dubbo://192.168.141.149:20880", timeout=60000, retries = 0)
	@Autowired
	private WelfareShopCallService welfareShopCallService;
	@Autowired
	private WelfareShopBasicMsgService welfareShopBasicMsgService;
	
	@Autowired
	private ProductServiceImpl productServiceImpl;
	
	/**
	 * 缓存获取单个商品详情
	 * @param pid 商品ID
	 * @return RpcResponse data 商品详情
	 */
	@Override
	public RpcResponse<ProductDetail> getProductDetailForCache(String pid) {
		RpcResponse<ProductDetail> response = new RpcResponse<ProductDetail>();
		try {
			ProductDetail productDetail = this.welfareShopBasicMsgService.getProductDetailFromCach(pid);
			response.setData(productDetail);
		} catch (Exception e) {
			log.error("福利商城【缓存获取单个商品详情】", e);
			this.exception(response,RpcResponse.SYSTEM_ERROR,"系统异常");
		}
		return response;
	}

	/**
	 * 获取单个地址详情
	 * @return RpcResponse data 地址信息
	 * @param code 地址code
	 */
	@Override
	public RpcResponse<Address> getAddress(String code){
		RpcResponse<Address> response = new RpcResponse<Address>();
		try {
			Address address = this.welfareShopBasicMsgService.getAddress(code);
			response.setData(address);
		} catch (Exception e) {
			log.error("福利商城【缓存获取单个地址】", e);
			this.exception(response,RpcResponse.SYSTEM_ERROR,"系统异常");
		}
		return response;
		
	}
	
	/**
	 * 获取地址
	 * @return RpcResponse data 地址集合
	 * @param province 省
	 * @param city 市
	 * @param county 县/区
	 */
	@Override
	public RpcResponse<List<Address>> getAddress(String province, String city,String county) {
		RpcResponse<List<Address>> response = new RpcResponse<List<Address>>();
		try {
			List<Address> addressList = this.welfareShopBasicMsgService.getAddress(province,city,county);
			response.setData(addressList);
		} catch (Exception e) {
			log.error("福利商城【缓存获取地址集合】", e);
			this.exception(response,RpcResponse.SYSTEM_ERROR,"系统异常");
		}
		return response;
	}
	
	/**
	 * 获取单个分类详情
	 * @return RpcResponse data 地址信息
	 * @param code 地址code
	 */
	@Override
	public RpcResponse<Cate> getCate(String code){
		RpcResponse<Cate> response = new RpcResponse<Cate>();
		try {
			Cate cate = this.welfareShopBasicMsgService.getCate(code);
			response.setData(cate);
		} catch (Exception e) {
			log.error("福利商城【缓存获取分类详情】", e);
			this.exception(response,RpcResponse.SYSTEM_ERROR,"系统异常");
		}
		return response;
		
	}

	/**
	 * 获取商品分类
	 * @param rootCate 一级分类
	 * @param twoCate 二级分类
	 * @return RpcResponse data 商品分类
	 * 
	 */
	@Override
	public RpcResponse<List<Cate>> getCates(String rootCate, String twoCate) {
		RpcResponse<List<Cate>> response = new RpcResponse<List<Cate>>();
		try {
			List<Cate> cateList = this.welfareShopBasicMsgService.getCates(rootCate,twoCate);
			response.setData(cateList);
		} catch (Exception e) {
			log.error("福利商城接口调用【获取单个商品详情】", e);
			this.exception(response,RpcResponse.SYSTEM_ERROR,"系统异常");
		}
		return response;
	}
	
	/**
	 * 单个查询商品库存
	 * @param pid 商品ID   必填
	 * @param num 数量   必填
	 * @param address 配送地址ID,格式：1_0_0 (分别代表1、2、3级地址)   必填
	 * @return RpcResponse data 商品库存
	 */
	@Override
	public RpcResponse<Stock> stock(String pid, String num, String address) {
		RpcResponse<Stock> response = new RpcResponse<Stock>();
		try {
			Stock stock = this.welfareShopCallService.stock(pid,num,address);
			response.setData(stock);
		} catch (BusinessException e) {
			this.exception(response,e.getCode(),e.getMessage());
		} catch (Exception e) {
			log.error("福利商城接口调用【单个查询商品库存】", e);
			this.exception(response,RpcResponse.SYSTEM_ERROR,"系统异常");
		}
		return response;
	}

	/**
	 * 批量查询商品库存
	 * @param pidNums 商品ID_数量， 多个使用逗号(,)拼接即可. 例如: 20169_1, 23908_2, 20167_3   必填
	 * @param address 配送地址ID,格式：1_0_0 (分别代表1、2、3级地址)   必填
	 * @return RpcResponse data 多个商品库存
	 */
	@Override
	public RpcResponse<List<Stock>> stockBatch(String pidNums, String address) {
		RpcResponse<List<Stock>> response = new RpcResponse<List<Stock>>();
		try {
			List<Stock> stocks = this.welfareShopCallService.stockBatch(pidNums,address);
			response.setData(stocks);
		} catch (BusinessException e) {
			this.exception(response,e.getCode(),e.getMessage());
		} catch (Exception e) {
			log.error("福利商城接口调用【批量查询商品库存】", e);
			this.exception(response,RpcResponse.SYSTEM_ERROR,"系统异常");
		}
		return response;
	}

	/**
	 * 查询商品可售状态
	 * @param pid 商品ID   必填
	 * @return RpcResponse data 商品可售状态
	 */
	@Override
	public RpcResponse<CommodityStatus> saleStatus(String pid) {
		RpcResponse<CommodityStatus> response = new RpcResponse<CommodityStatus>();
		try {
			CommodityStatus commodityStatus = this.welfareShopCallService.saleStatus(pid);
			response.setData(commodityStatus);
		} catch (BusinessException e) {
			this.exception(response,e.getCode(),e.getMessage());
		} catch (Exception e) {
			log.error("福利商城接口调用【查询商品可售状态】", e);
			this.exception(response,RpcResponse.SYSTEM_ERROR,"系统异常");
		}
		return response;
	}

	/**
	 * 批量查询商品可售状态
	 * @param pids 商品ID,多个使用逗号(,)连接即可。例如: pid =20169,20178,23908   必填
	 * @return RpcResponse data 多个商品可售状态
	 */
	@Override
	public RpcResponse<List<CommodityStatus>> batchSaleStatus(String pids) {
		RpcResponse<List<CommodityStatus>> response = new RpcResponse<List<CommodityStatus>>();
		try {
			List<CommodityStatus> commodityStatus = this.welfareShopCallService.batchSaleStatus(pids);
			response.setData(commodityStatus);
		} catch (BusinessException e) {
			this.exception(response,e.getCode(),e.getMessage());
		} catch (Exception e) {
			log.error("福利商城接口调用【批量查询商品可售状态】", e);
			this.exception(response,RpcResponse.SYSTEM_ERROR,"系统异常");
		}
		return response;
	}

	/**
	 * 查询商品协议价
	 * @param pid 商品ID   必填
	 * @return RpcResponse data 商品协议价
	 */
	@Override
	public RpcResponse<Price> getPrice(String pid) {
		RpcResponse<Price> response = new RpcResponse<Price>();
		try {
			Price price = this.welfareShopCallService.getPrice(pid);
			response.setData(price);
		} catch (BusinessException e) {
			this.exception(response,e.getCode(),e.getMessage());
		} catch (Exception e) {
			log.error("福利商城接口调用【查询商品协议价】", e);
			this.exception(response,RpcResponse.SYSTEM_ERROR,"系统异常");
		}
		return response;
	}

	/**
	 * 批量查询商品协议价
	 * @param pids 商品ID,多个使用逗号(,)连接即可。例如: pid =20169,20178,23908   必填
	 * @return RpcResponse data 多个商品协议价
	 */
	@Override
	public RpcResponse<List<Price>> batchGetPrice(String pids) {
		RpcResponse<List<Price>> response = new RpcResponse<List<Price>>();
		try {
			List<Price> prices = this.welfareShopCallService.batchGetPrice(pids);
			response.setData(prices);
		} catch (BusinessException e) {
			this.exception(response,e.getCode(),e.getMessage());
		} catch (Exception e) {
			log.error("福利商城接口调用【批量查询商品协议价】", e);
			this.exception(response,RpcResponse.SYSTEM_ERROR,"系统异常");
		}
		return response;
	}

	/**
	 * 提交订单
	 * @param createOrderParam 创建订单参数
	 * @return RpcResponse data 提交订单结果集
	 */
	@Override
	public RpcResponse<CreateOrderResult> submitOrder(
			CreateOrderParam createOrderParam) {
		RpcResponse<CreateOrderResult> response = new RpcResponse<CreateOrderResult>();
		try {
			CreateOrderResult createOrderResult = this.welfareShopCallService.submitOrder(createOrderParam);
			response.setData(createOrderResult);
		} catch (BusinessException e) {
			this.exception(response,e.getCode(),e.getMessage());
		} catch (Exception e) {
			log.error("福利商城接口调用【创建订单】", e);
			this.exception(response,RpcResponse.SYSTEM_ERROR,"系统异常");
		}
		return response;
	}

	/**
	 * 查询订单详情
	 * @param orderKey 礼魔方系统订单号   必填
	 * @return RpcResponse data 订单详情 
	 */
	@Override
	public RpcResponse<OrderDetail> orderDetail(String orderKey) {
		RpcResponse<OrderDetail> response = new RpcResponse<OrderDetail>();
		try {
			OrderDetail orderDetail = this.welfareShopCallService.orderDetail(orderKey);
			response.setData(orderDetail);
		} catch (BusinessException e) {
			this.exception(response,e.getCode(),e.getMessage());
		} catch (Exception e) {
			log.error("福利商城接口调用【查询订单详情】", e);
			this.exception(response,RpcResponse.SYSTEM_ERROR,"系统异常");
		}
		return response;
	}

	/**
	 * 订单反查询, 用于确认订单是否创建成功
	 * @param thirdOrder 我方生成系统订单号   必填
	 * @return RpcResponse data 礼魔方订单号
	 */
	@Override
	public RpcResponse<String> orderSure(String thirdOrder) {
		RpcResponse<String> response = new RpcResponse<String>();
		try {
			String orderNo = this.welfareShopCallService.orderSure(thirdOrder);
			response.setData(orderNo);
		} catch (BusinessException e) {
			this.exception(response,e.getCode(),e.getMessage());
		} catch (Exception e) {
			log.error("福利商城接口调用【订单反查询】", e);
			this.exception(response,RpcResponse.SYSTEM_ERROR,"系统异常");
		}
		return response;
	}

	/**
	 * 订单物流信息接口-根据我方订单号获取
	 * @param thirdOrder 我方生成系统订单号   必填
	 * @return RpcResponse data 订单物流信息
	 */
	@Override
	public RpcResponse<LogisticsMsg> orderTrack(String thirdOrder) {
		RpcResponse<LogisticsMsg> response = new RpcResponse<LogisticsMsg>();
		try {
			LogisticsMsg logisticsMsg = this.welfareShopCallService.orderTrack(thirdOrder);
			response.setData(logisticsMsg);
		} catch (BusinessException e) {
			this.exception(response,e.getCode(),e.getMessage());
		} catch (Exception e) {
			log.error("福利商城接口调用【订单物流信息接口-根据我方订单号】", e);
			this.exception(response,RpcResponse.SYSTEM_ERROR,"系统异常");
		}
		return response;
	}

	/**
	 * 订单物流信息接口-根据礼魔方订单号获取
	 * @param orderKey 礼魔方系统订单号
	 * @return RpcResponse data 订单物流信息
	 */
	@Override
	public RpcResponse<LogisticsMsg> systemOrderTrack(String orderKey) {
		RpcResponse<LogisticsMsg> response = new RpcResponse<LogisticsMsg>();
		try {
			LogisticsMsg logisticsMsg = this.welfareShopCallService.systemOrderTrack(orderKey);
			response.setData(logisticsMsg);
		} catch (BusinessException e) {
			this.exception(response,e.getCode(),e.getMessage());
		} catch (Exception e) {
			log.error("福利商城接口调用【订单物流信息接口-根据礼魔方订单号】", e);
			this.exception(response,RpcResponse.SYSTEM_ERROR,"系统异常");
		}
		return response;
	}
	
	/**
	 * 查询预存款余额
	 * @return RpcResponse data 预存款余额信息
	 */
	@Override
	public RpcResponse<Remain> remain(){
		RpcResponse<Remain> response = new RpcResponse<Remain>();
		try {
			Remain remain = this.welfareShopCallService.remain();
			response.setData(remain);
		} catch (BusinessException e) {
			this.exception(response,e.getCode(),e.getMessage());
		} catch (Exception e) {
			log.error("福利商城接口调用【查询预存款余额】", e);
			this.exception(response,RpcResponse.SYSTEM_ERROR,"系统异常");
		}
		return response;
	}

	/**
	 * 获取商品退货地址信息
	 * @param pid 商品ID
	 * @return RpcResponse data 商品退货信息
	 */
	@Override
	public RpcResponse<SalesReturnMsg> getReturnAddress(String pid) {
		RpcResponse<SalesReturnMsg> response = new RpcResponse<SalesReturnMsg>();
		try {
			SalesReturnMsg salesReturnMsg = this.welfareShopCallService.getReturnAddress(pid);
			response.setData(salesReturnMsg);
		} catch (BusinessException e) {
			this.exception(response,e.getCode(),e.getMessage());
		} catch (Exception e) {
			log.error("福利商城接口调用【获取商品退货地址信息】", e);
			this.exception(response,RpcResponse.SYSTEM_ERROR,"系统异常");
		}
		return response;
	}

	/**
	 * 查询订单商品是否可以进行退换货操作
	 * @param orderKey 礼魔方系统订单号
	 * @param productIds 商品ID,如果有多个使用逗号(,)拼接, 例如: 232391,348273,58723
	 * @return RpcResponse data  商品是否可以进行退换货操作信息集合
	 */
	@Override
	public RpcResponse<List<CommodityStatus>> getStatus(String orderKey,
			String productIds) {
		RpcResponse<List<CommodityStatus>> response = new RpcResponse<List<CommodityStatus>>();
		try {
			List<CommodityStatus> commodityStatus = this.welfareShopCallService.getStatus(orderKey,productIds);
			response.setData(commodityStatus);
		} catch (BusinessException e) {
			this.exception(response,e.getCode(),e.getMessage());
		} catch (Exception e) {
			log.error("福利商城接口调用【查询订单商品是否可以进行退换货操作】", e);
			this.exception(response,RpcResponse.SYSTEM_ERROR,"系统异常");
		}
		return response;
	}

	/**
	 * 申请退货
	 * @param SalesReturnOrNifferApplyParam 退货 参数
	 * @return RpcResponse data 申请退货返回结果信息
	 */
	@Override
	public RpcResponse<SalesReturnOrNifferApplyResult> salesReturn(
			SalesReturnOrNifferApplyParam salesReturnApplyParam) {
		RpcResponse<SalesReturnOrNifferApplyResult> response = new RpcResponse<SalesReturnOrNifferApplyResult>();
		try {
			SalesReturnOrNifferApplyResult result = this.welfareShopCallService.salesReturnOrNiffer(salesReturnApplyParam,"return");
			response.setData(result);
		} catch (BusinessException e) {
			this.exception(response,e.getCode(),e.getMessage());
		} catch (Exception e) {
			log.error("福利商城接口调用【申请退货】", e);
			this.exception(response,RpcResponse.SYSTEM_ERROR,"系统异常");
		}
		return response;
	}

	/**
	 * 申请换货
	 * @param SalesReturnOrNifferApplyParam 换货 参数
	 * @return RpcResponse data 申请换货返回结果信息
	 */
	@Override
	public RpcResponse<SalesReturnOrNifferApplyResult> salesNiffer(
			SalesReturnOrNifferApplyParam salesReturnApplyParam) {
		RpcResponse<SalesReturnOrNifferApplyResult> response = new RpcResponse<SalesReturnOrNifferApplyResult>();
		try {
			SalesReturnOrNifferApplyResult result = this.welfareShopCallService.salesReturnOrNiffer(salesReturnApplyParam,"niffer");
			response.setData(result);
		} catch (BusinessException e) {
			this.exception(response,e.getCode(),e.getMessage());
		} catch (Exception e) {
			log.error("福利商城接口调用【申请换货】", e);
			this.exception(response,RpcResponse.SYSTEM_ERROR,"系统异常");
		}
		return response;
	}
	
	/**
	 * 售后订单详情
	 * @param orderKey 礼魔方系统订单号
	 * @return RpcResponse data 售后订单详情
	 */
	@Override
	public RpcResponse<AfterSaleDetail> afterSaleDetail(String orderKey) {
		RpcResponse<AfterSaleDetail> response = new RpcResponse<AfterSaleDetail>();
		try {
			AfterSaleDetail afterSaleDetail = this.welfareShopCallService.afterSaleDetail(orderKey);
			response.setData(afterSaleDetail);
		} catch (BusinessException e) {
			this.exception(response,e.getCode(),e.getMessage());
		} catch (Exception e) {
			log.error("福利商城接口调用【售后订单详情】", e);
			this.exception(response,RpcResponse.SYSTEM_ERROR,"系统异常");
		}
		return response;
	}
	
	private void exception(RpcResponse<?> response,String errorCode,String message) {
		response.setResultCode(errorCode);
		response.setResultMsg(message);
	}
	/**
	 * 全局搜索商品
	 */
	@Override
	public RpcResponse<Page<ProductQuery, ProductDetail>> queryEsSearchProductByPage(Page<ProductQuery, ProductDetail> Page) {
		RpcResponse<Page<ProductQuery, ProductDetail>> response = new RpcResponse<Page<ProductQuery, ProductDetail>>();
		try {
			Page<ProductQuery, ProductDetail> productDetails = this.productServiceImpl.queryEsSearchProductByPage(Page);
			response.setData(productDetails);
		} catch (BusinessException e) {
			this.exception(response,e.getCode(),e.getMessage());
		} catch (Exception e) {
			log.error("福利商城接口调用【全局搜索商品】", e);
			this.exception(response,response.SYSTEM_ERROR,"系统异常");
		}
		return response;
	}

	@Override
	public RpcResponse<Page<ProductQuery, ProductDetail>> getProductsByPage(Page<ProductQuery, ProductDetail> query) {
		RpcResponse<Page<ProductQuery, ProductDetail>> response = new RpcResponse<Page<ProductQuery, ProductDetail>>();
		try {
			Page<ProductQuery, ProductDetail> productDetails = this.welfareShopBasicMsgService.queryProductsByPage(query);
			response.setData(productDetails);
		} catch (Exception e) {
			log.error("福利商城【查询商品列表】错误", e);
			this.exception(response,RpcResponse.SYSTEM_ERROR,"系统异常");
		}
		return response;
	}
	
	@Override
	public RpcResponse<Page<ProductQuery, ProductDetail>> queryEsSearchProductByPageSort(Page<ProductQuery, ProductDetail> query) {
		RpcResponse<Page<ProductQuery, ProductDetail>> response = new RpcResponse<Page<ProductQuery, ProductDetail>>();
		try {
			Page<ProductQuery, ProductDetail> productDetails = this.productServiceImpl.queryEsSearchProductByPageSort(query);
			response.setData(productDetails);
		} catch (Exception e) {
			log.error("福利商城【按类型查询商品列表】错误", e);
			this.exception(response,RpcResponse.SYSTEM_ERROR,"系统异常");
		}
		return response;
	}

	@Override
	public RpcResponse<List<Cate>> queryCates(List<String> cateList) {
		RpcResponse<List<Cate>> result = new RpcResponse<List<Cate>>();
		if(null == cateList || cateList.isEmpty()){
			return result;
		}
		List<Cate> cates = this.welfareShopBasicMsgService.queryCateList(cateList);
		result.setData(cates);
		return result;
	}

}
